package com.lw.smartdevice.controller;

import com.github.pagehelper.PageInfo;
import com.lw.smartdevice.dto.R;
import com.lw.smartdevice.pojo.SysCategory;
import com.lw.smartdevice.service.SysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/3/20 20:13
 */
@RestController
@RequestMapping("/categories")
public class CategoryController {

    private SysCategoryService sysCategoryService;

    @Autowired
    public void setSysCategoryService(SysCategoryService sysCategoryService) {
        this.sysCategoryService = sysCategoryService;
    }

    @GetMapping("/category/{id}")
    public R findById(@PathVariable Long id){
        SysCategory sysCategory=sysCategoryService.findById(id);
        return R.setOK(sysCategory);
    }

    @PostMapping("/category")
    public R addSysCategory(@RequestBody SysCategory category){
        sysCategoryService.addSysCategory(category);
        return R.setOK();
    }

    @GetMapping("/categories")
    public R findAllByPage(@RequestParam(defaultValue = "1",name = "page") int pageNum,@RequestParam(defaultValue = "1",name = "limit") int pageSize,String categoryName){
        PageInfo<SysCategory> pageInfo = sysCategoryService.findAll(pageNum, pageSize, categoryName);
        return R.setOK(pageInfo);
    }

    @PostMapping("/update")
    public R updateCategory(@RequestBody SysCategory category){
        int count = sysCategoryService.updateCategory(category);
        return R.setOK(count);
    }

    @PostMapping("/delete")
    public R deleteCategory(@RequestBody List<Long> ids){
        int count = sysCategoryService.deleteByIds(ids);
        return R.setOK(count);
    }
}
